#!/usr/bin/python

# Locsi Control functions based on
# Locsi Database Interfacing functions and
# Google Calendar API v3 Resource representations and interfacing functions

#--------------------------------
# LocsiCTRL API
#--------------------------------
#
# Assumptions:  sqlite db file exists
#               LocsiDB has been initialized - LocsiDB.initdbfromGC()
#               LocsiDB.dbtable is 'Events'
#
# Global variables
#   LocsiCTRL.con - connection to LocsiDB, to be initialized with
#               LocsiCTRL.initLocsiCTRLdb(LocsiDB.dbpath) - get con
#               LocsiCTRL.initLocsiCTRLdb(LocsiDB.dbpath,1) - get con and create new LocsiCTRL table
#   LocsiCTRL.GCBacklog - counter of status updates attempted to be sent
#               to Google Calendar with no success
#
# API Functions
#   def initLocsiCTRLdb(dbpath = None, droptable = 0)
#       initialize LocsiCTRL.con global, used by all CTRL functions below
#       when droptable is 1 a new LocsiCTL table is created in LocsiDB.dbpath database
#
#   def loop()
#       starts the control loop which is executing events
#       - that are in the Events table - initialized by LocsiDB.initdbfromGC()
#       - selection of next event to execute is determined by loop type - see loopType() below
#       - execution state is controlled by the value of the 'status' field in LocsiCTRL table of LocsiDB
#           'Exit' - if status field is set to 'Exit' the control loop exits, execution is terminated, currently executed event is stopped
#           'Stopped' - execution of events is stopped, control loop continues running, waits for a change in status while event execution is paused
#           'Running' - executing events with selection and timing specified by loop type (see loopType())
#       - status is initialized to 'Exit' when LocsiCTRL table is created with initLocsiCTRLdb(dbpath, 1)
#       - to enable execution first set status to 'Running' with run()
#       - WARNING: - DOES NOT RETURN PROMPT IN A TERMINAL UNTIL EXIT
#       assumes initialized LocsiCTRL.con global
#
#   def run()
#       sets LocsiCTRL state to 'Running'
#       for executing events control loop must be running (see loop())
#       assumes initialized LocsiCTRL.con global
#
#   def stop()
#       sets LocsiCTRL state to 'Stopped'
#       assumes initialized LocsiCTRL.con global
#
#   def exitloop()
#       sets LocsiCTRL state to 'Exit'
#       assumes initialized LocsiCTRL.con global
#
#   def getstate()
#       sets LocsiCTRL state to 'Running'
#       assumes initialized LocsiCTRL.con global
#
#   def loopType( newLoopType = '' )
#       returns current LoopType with no arguments given
#       sets LoopType according the argument
#           REALTIME 
#               return first action not DONE yet to be started now or later
#               interrupted execution is not restarted,
#               in case of overlapping events some are left out
#           QUEUED
#               return first action that is not DONE yet
#               in the specified timewindow of the LocsiDB
#               all events executed
#               interrupted execution continued when restarted if still in the timewindow
#               start time is not guaranteed, started when the last preceding event is done
#       assumes initialized LocsiCTRL.con global
#

import LocsiDB
import LocsiCTRL

con = LocsiCTRL.initLocsiCTRLdb(LocsiDB.dbpath,1)
LocsiCTRL.run()
LocsiCTRL.loop()

#print getstate()
#run()
#print getstate()
#stop()
#print getstate()
#exitloop()
#print getstate()

#LocsiCTRL.getstate()
#LocsiCTRL.run()
#LocsiCTRL.getstate()
#LocsiCTRL.stop()
#LocsiCTRL.getstate()
#LocsiCTRL.exitloop()
#LocsiCTRL.getstate()

"""
import LocsiCTRL
import LocsiDB

LocsiDB.initdbfromGC()
LocsiCTRL.initLocsiCTRLdb(LocsiDB.dbpath)
LocsiCTRL.run()
LocsiCTRL.loop()

reload(LocsiCTRL)

LocsiCTRL.initLocsiCTRLdb(LocsiDB.dbpath)
LocsiCTRL.run()
LocsiCTRL.loop()

sqlite console:

SELECT Id, Progress, Done FROM Events;
SELECT Id, Progress, Done FROM Events;

UPDATE LocsiCTRL SET State = "Stop";

SELECT Id, Progress, Done FROM Events;
SELECT Id, Progress, Done FROM Events;

UPDATE LocsiCTRL SET State = "Running";

SELECT Id, Progress, Done FROM Events;
SELECT Id, Progress, Done FROM Events;

UPDATE LocsiCTRL SET State = "Exit";
"""
